cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(Sensor)

message(${CMAKE_CURRENT_SOURCE_DIR})

option(CXX11_ABI "whether to use CXX11_ABI libtorch" OFF)
option(GRAPH_TOOLS "whether to build TLG graph tools" OFF)
option(BUILD_TESTING "whether to build unit test" OFF)

option(GRPC "whether to build with gRPC" OFF)
# TODO(Binbin Zhang): Change websocket to OFF since it depends on boost
#                     which is a very big library
option(WEBSOCKET "whether to build with websocket" OFF)
option(HTTP "whether to build with http" OFF)
option(TORCH "whether to build with Torch" OFF)
option(ONNX "whether to build with ONNX" ON)
option(GPU "whether to build with GPU" OFF)
option(ITN "whether to use WeTextProcessing" ON)

set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -g")
set(CMAKE_VERBOSE_MAKEFILE OFF)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  cv_bridge
  image_transport
  message_generation
)

find_package(nlohmann_json REQUIRED)

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES Sensor
  CATKIN_DEPENDS roscpp rospy std_msgs
  DEPENDS system_lib
)

include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

include_directories(/opt/MVS/include/include)
link_directories(/opt/MVS/lib/64)

include(FetchContent)
set(FETCHCONTENT_QUIET OFF)
get_filename_component(fc_base "fc_base" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(FETCHCONTENT_BASE_DIR ${fc_base})

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

if(NOT MSVC)
  # Keep the same with openfst, -fPIC or -fpic
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fPIC")
else()
  set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
endif()

# Include all dependency
if(TORCH)
  include(libtorch)
endif()
if(ONNX)
  include(onnx)
endif()

include(openfst)
include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}/kaldi
)

if(ITN)
  include(wetextprocessing)
endif()

# Build all libraries
add_subdirectory(utils)
add_subdirectory(frontend)
add_subdirectory(post_processor)
add_subdirectory(kaldi)  # kaldi: wfst based decoder
add_subdirectory(decoder)
add_subdirectory(api)

# Build all bins
# add_subdirectory(bin)

add_executable(Sensor src/main.cpp src/ImageSave.cpp)
# target_link_libraries(Sensor PUBLIC decoder portaudio)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format-security")

target_link_libraries(Sensor
  PUBLIC decoder portaudio
  curl
  nlohmann_json
  yaml-cpp
  MvCameraControl
  pthread
  #opencv
  ${catkin_LIBRARIES}

)


# Unit Test
if(BUILD_TESTING)
  include(gtest)
  add_subdirectory(test)
endif()
